package org.redr.widgetGUI.settings.settingsTools;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ItemsHandler {
	Element e;
	
	public ItemsHandler(Element el){
		e = el;
	}
	
	public void addItem(String key, String value){
		Element i = e.getOwnerDocument().createElement("Item");
		e.appendChild(i);
		i.setAttribute("key", key);
		i.setAttribute("value", value);
//		System.out.println("Item node");
//		System.out.println(XMLSetting.xmlToString(e));
//		System.out.println("Parent Node");
//		System.out.println(XMLSetting.xmlToString(e.getParentNode()));
//		System.out.println("Parent Document");
//		System.out.println(XMLSetting.xmlToString(e.getOwnerDocument()));
		
	}
	
	public void setItem(String key, String value){
		int index = itemIndex(key);
		if (index >= 0){
			Element el = (Element) e.getElementsByTagName("Item").item(index);
			el.setAttribute(key, value);
		}else{
			addItem(key, value);
		}
	}
	
	public void clear(){
		while(e.hasChildNodes()){
			e.removeChild(e.getFirstChild());
		}
	}
	
	public void setItems(Map<String, String> names){
		clear();
		for(String s : names.keySet()){
			addItem(s, names.get(s));
		}
	}
	
	public void setDefaultItem(String key, String value){
//		System.out.println("Items handler set default item");
		int index = itemIndex(key);
		if (index < 0) {
//			System.out.println("Adding " + key +": " + value);
			addItem(key, value);
		}
		else {
//			System.out.println(index + " " + key + " " + value); 
//			System.out.println(XMLSetting.xmlToString(e.getElementsByTagName("Item").item(index)));
			return;
		}
	}
	
	public void setDefaultItems(String ... items){
		for (String s : items){
			String[] kv = s.split("||");
			if (kv.length > 1) setDefaultItem(kv[0], kv[1]);
			else setDefaultItem(s, s);
		}
	}
	
	public boolean containsItem(String key){
		if (itemIndex(key) >= 0) return true;
		else return false;
	}
	
	public int itemIndex(String key){
		NodeList nl = e.getElementsByTagName("Item");
		for (int n = 0; n < nl.getLength(); n++){
			Element el = (Element) nl.item(n);
			if (el.getAttribute("key").equals(key)) return n;
		}
		return -1;
	}
	
	public String[] getKeys(){
		System.out.println("Getting keys");
		NodeList nl = e.getElementsByTagName("Item");
		String[] result = new String[nl.getLength()];
		for (int n = 0; n < nl.getLength(); n++){
			result[n] = ((Element) nl.item(n)).getAttribute("key");
//			System.out.println(result[n]);
		}
		return result;
	}
	
	public String getValue(String key){
		int index = itemIndex(key);
		if (index < 0) return "";
		else{
			return ((Element) e.getElementsByTagName("Item").item(index)).getAttribute("value");
		}
	}
	
	public HashMap<String, String> getKeyValuePairs(){
		String[] keys = getKeys();
		HashMap<String, String> result = new HashMap<String, String>(keys.length);
		for (String k : keys){
			result.put(k, getValue(k));
		}
		return result;
	}

}
